<?php
class SecQuestion extends Controller 
{
	var $uid;
	var $tpl;

	function setVars()
	{
		$this->uid = $this->session->userdata('uid');
		$this->tpl['title'] = "Security Questions";
		$this->tpl['head'] = '';
        $this->tpl['sidebar'] = 'eMail address change. We need another form to ID you. Please answer at lased 3 questions.';
        $this->sq = security_quetions();
		$this->count = 0;
        $this->dbget();
	}
	
	function SecQuestion()
	{
		parent::Controller();
        $this->auth->CheckLogin();
		$this->setVars();
		log_message('debug', "SecQuestion: Controler Class Initialized");
	}


/*****************************************************************\
|               Sequrity Question
\*****************************************************************/	

	function index() 
	{
        $this->tpl['sq']  = $this->sq;

		if(isset($_POST['sq']))
		{
			$this->postCheck();
		}
		//$this->tpl['myQA'] = $this->get_security_answers();
		$this->load->view('contact/secquestion', $this->tpl);
	}

	function postCheck()
	{
        foreach($this->sq as $k => $v){
            $kk = str_replace(' ', '_', $k);
            if($this->input->post($kk) != 0){
                $kp['SecQA'][$kk] = $this->input->post($kk);
            }
        }
        if(count($kp['SecQA']) < 3) {
            $this->msg->setError('Too few answers');
        }else{
            $this->dbsq = $kp['SecQA'];
            $this->dbadd($kp);
            $this->answers();
        }
	}

    
    function answers()
    {
        $this->tpl['dbsq'] = '<dl>';
        foreach($this->dbsq as $k=>$v){
            $k = str_replace('_', ' ', $k);
            $v = $this->sq[$k][$v];
            $k = $k.'?';
            $this->tpl['dbsq'] .= '<dt>'.$k.'</dt><dd>'.$v.'</dd>';
        }
        $this->tpl['dbsq'] .= '</dl>';
    }
    
    function dbget(){
        $this->db->select('vars');
        $this->db->where('uid', $this->uid);
        $query = $this->db->get('contact_user');
        if ($query->num_rows() > 0)
        {
            $row = $query->row();
            $array = unserialize($row->vars);
            $this->dbsq = $array['SecQA'];
            $this->answers();
        }

    }
    
    function dbadd($data){
        $d['vars'] = serialize($data);
        $this->db->where('uid', $this->uid);
        $this->db->update('contact_user', $d);
    }
}
?>
